<?php

/**
 * @author ngockv@gmail.com
 * @copyright 2012
 */

function image_upload($image_name, $default=false){
  $dir = 'files/'.date('Ymd', time());
  file_check_dir($dir);
  
  $path = $dir.'/'.time()."_".$_FILES[$image_name]['name'];
  $url = PG_ROOT.$path;
  
 //extensive suitability check before doing anything with the file...
  if (($_FILES[$image_name] == "none") OR (empty($_FILES[$image_name]['name'])) ){
   return $default;
  }else if ($_FILES[$image_name]["size"] == 0){
   return $default;
  }else if (($_FILES[$image_name]["type"] != "image/pjpeg") AND ($_FILES[$image_name]["type"] != "image/jpeg") AND ($_FILES[$image_name]["type"] != "image/png")){
   return $default;
  }else if (!is_uploaded_file($_FILES[$image_name]["tmp_name"])){
   return $default;
  } else {
  $move = @ move_uploaded_file($_FILES[$image_name]['tmp_name'], $url);
  if(!$move){
    return $default;
  }
  }
  
  return $path;
}

function file_check_dir($dir){
  $path = PG_ROOT.$dir;
  if (!file_exists($path)){
  $aPath = explode('/', $dir); array_pop($aPath);
  file_check_dir(implode('/', $aPath));
  @mkdir($path); chmod($path, 0777);
  }
}

function image_thumb_settings(){
  return array(
   'scale160x120' => array(
  'type'  => 'scale',
  'width'   => 160,
  'height'  => 120
   ),
   'scale160x160' => array(
  'type'  => 'scale',
  'width'   => 160,
  'height'  => 160
   ),
   'scale450x300' => array(
  'type'  => 'scale',
  'width'   => 450,
  'height'  => 300
   ),
   'crop290x245' => array(
  'type'  => 'crop',
  'width'   => 290,
  'height'  => 245
   ),
   'h45' => array(
  'type'  => 'scale',
  'width'   => 0,
  'height'  => 45
   ),
   'crop85x65' => array(
  'type'  => 'crop',
  'width'   => 85,
  'height'  => 65
   ),
   'scale90x70' => array(
  'type'  => 'scale',
  'width'   => 90,
  'height'  => 70
   ),
   'crop385x250' => array(
  'type'  => 'crop',
  'width'   => 385,
  'height'  => 250
   ),
   'crop300x300' => array(
    'type'    => 'crop',
    'width'   => 385,
    'height'  => 250
   ),
   'h65' => array(
  'type'  => 'scale',
  'width'   => 0,
  'height'  => 65
   ),
   'w250' => array(
    'type'    => 'scale',
    'width'   => 250,
    'height'  => 0
   ),
   'crop300x375' => array(
    'type'    => 'crop',
    'width'   => 300,
    'height'  => 375
   ),
   'crop291x247' => array(
    'type'    => 'crop',
    'width'   => 291,
    'height'  => 247
   )
   // New Template
   ,
   'rc110x110' => array(
    'type'    => 'resizecrop',
    'width'   => 110,
    'height'  => 110
   ),
   'crop202x148' => array(
    'type'    => 'crop',
    'width'   => 202,
    'height'  => 148
   ),
   'crop266x260' => array(
    'type'    => 'crop',
    'width'   => 266,
    'height'  => 260
   ),
   'crop474x474' => array(
    'type'    => 'crop',
    'width'   => 474,
    'height'  => 474
   ),
   'crop110x110' => array(
    'type'    => 'crop',
    'width'   => 110,
    'height'  => 110
   ),
   'crop556x260' => array(
    'type'    => 'crop',
    'width'   => 556,
    'height'  => 260
   ),
   'crop266x260' => array(
    'type'    => 'crop',
    'width'   => 266,
    'height'  => 260
   )
  );
}

function image_thumb($path, $thumb){
	global $oAwsCdn;
	
  $thumbSetting = image_thumb_settings();
  if (!isset($thumbSetting[$thumb])) return $path;
  
  $aPath = explode('/', $path);
  $files = array_shift($aPath);
  if ($files != 'files') return $path;
  $url = $oAwsCdn->getUrlFromFilePath($path, $thumb);
  if ($url = $oAwsCdn->getUrlFromFilePath($path, $thumb))	return $url;

  return 'files/thumb/'.$thumb.'/'.implode('/', $aPath);
}

function imagealphamask( &$picture, $mask ) {
  // Get sizes and set up new picture
  $xSize = imagesx( $picture );
  $ySize = imagesy( $picture );
  $newPicture = imagecreatetruecolor( $xSize, $ySize );
  imagesavealpha( $newPicture, true );
  imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );

  // Resize mask if necessary
  if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
    $tempPic = imagecreatetruecolor( $xSize, $ySize );
    imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
    imagedestroy( $mask );
    $mask = $tempPic;
  }

  // Perform pixel-based alpha map application
  for( $x = 0; $x < $xSize; $x++ ) {
    for ($y = 0; $y < $ySize; $y++) {
      $alpha = imagecolorsforindex($mask, imagecolorat($mask, $x, $y));
      $alpha = 127 - floor($alpha['red'] / 2);
      if (127 == $alpha) {
        continue;
      }
      $color = imagecolorsforindex($picture, imagecolorat($picture, $x, $y));
      imagesetpixel($newPicture, $x, $y, imagecolorallocatealpha(
      $newPicture, $color['red'], $color['green'], $color['blue'], $alpha));
    }
  }

  // Copy back to original picture
  imagedestroy( $picture );
  $picture = $newPicture;
}

?>